/*
 * WaitTradeItemLookup.cpp
 *
 *  Created on: 2014. 3. 28.
 *      Author: mega
 */

#include "../Static.h"
#include "../../NLogic/NAsync/AsyncTrade.h"
#include "WaitTradeItemLookup.h"

namespace ProjectZ { namespace NState { namespace NWait {

WaitTradeItemLookup::WaitTradeItemLookup()
{
	_name = "WAITTRADEITEMLOOKUPCOUNT";
}

WaitTradeItemLookup::~WaitTradeItemLookup()
{
}

void
WaitTradeItemLookup::OnClose(User * pUser, bool bForceClose)
{
	pUser->SetDisconnectStatus(true);
}

void
WaitTradeItemLookup::OnResponse(::Database::Command * pCommand)
{
	User * pUser = static_cast<User *>(pCommand->GetObserver());

	NLogic::NAsync::AsyncTrade * pAsyncTrade = pUser->GetAsyncTrade();
	if (NULL == pAsyncTrade)
	{
		SendAck(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	if (true == pUser->GetDisconnectStatus())
	{
		pUser->OnDisconnect();
		return;
	}

	if (DB_TRADEITEM_LOOKUP_COUNT == pCommand->GetCommandId())
	{
		if (0 != pCommand->GetErrorIdx())
		{
			SendAck(pUser, ACK_DB_ERROR);
			return;
		}

		int total_count = 0;

		while (pCommand->Next())
		{
			pCommand->GetRecord() >> total_count;
		}

//		DEBUGLOG("total_count = %d", total_count);

		if (0 >= total_count)
		{
			SendAck(pUser, ACK_OK);
			return;
		}

		pAsyncTrade->SetTotalCount(total_count);

		{
			Database::Command * pNewCommand = Database::GETCOMMANDPOOL()->NEW();

			pNewCommand->SetQueryType(::Database::Command::S_PROCEDURE);
			pNewCommand->MakeQuery("call sp_select_trade_item_v2(%d,%d,%d,%d,%d,%d,%d,%d)",
					pAsyncTrade->GetData().class_type, pAsyncTrade->GetData().sheet_type,
					pAsyncTrade->GetData().min_level, pAsyncTrade->GetData().max_level,
					pAsyncTrade->GetData().quality, pAsyncTrade->GetData().start_index,
					pAsyncTrade->GetData().end_index, pUser->GetUserSeq());
			pNewCommand->SetObserver(pUser);
			pNewCommand->SetCommandId(DB_TRADEITEM_LOOKUP);

			if (false == Database::GETSESSION()->AsyncExecute(static_cast<int>(DatabaseType::TRADE_SLAVE), pNewCommand))
			{
				SendAck(pUser, ACK_DB_ERROR);
				Database::GETCOMMANDPOOL()->DEL(pCommand);
				return;
			}
		}

	}
	else if (DB_TRADEITEM_LOOKUP == pCommand->GetCommandId())
	{
		if (0 != pCommand->GetErrorIdx())
		{
			DEBUGLOG("%s : Trade Item Lookup Error=%d", __FUNCTION__, pCommand->GetErrorIdx());
			SendAck(pUser, ACK_DB_ERROR);
			return;
		}

		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		*pAck << static_cast<int16_t>(ACK_OK);
		*pAck << static_cast<uint16_t>(pAsyncTrade->GetTotalCount());
		*pAck << static_cast<uint8_t>(pCommand->ResultRowCount());

		while (pCommand->Next())
		{
			char refine_step	= 0;
			char level			= 0;
			char quality		= 0;
			char stone_slot		= 0;
			char evolve_step	= 0;
			char evolve_max		= 0;
			char eff_type0		= 0;
			char remain_hour	= 0;
			char set_type		= 0;
			uint64_t trade_seq	= 0;
			uint16_t tid		= 0;
			uint16_t eff_value0	= 0;
			int32_t item_price	= 0;
			Toolkit::Str<81> nickname;

			pCommand->GetRecord() >> trade_seq >> item_price >> tid >> refine_step
					>> quality >> stone_slot >> evolve_step >> evolve_max
					>> eff_type0 >> eff_value0 >> nickname >> level >> remain_hour >> set_type;

			*pAck << trade_seq << static_cast<int8_t>(pAsyncTrade->GetData().class_type)
					<< static_cast<int8_t>(pAsyncTrade->GetData().sheet_type)
					<< tid << refine_step << level << quality << stone_slot << evolve_step << evolve_max
					<< eff_type0 << eff_value0 << item_price << nickname.c_str() << remain_hour << set_type;
		}

		pAck->MakePacket(CMD_SC_MARKET_ITEM_SALE_LIST_V2);
		pUser->Send(pAck);

		pUser->SetState(Static::Instance().MAINFRIENDLIST());
	}
}

void
WaitTradeItemLookup::SendAck(User * pUser, int ack)
{
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	*pAck << static_cast<int16_t>(ack);
	*pAck << static_cast<uint16_t>(0);
	*pAck << static_cast<uint8_t>(0);
	pAck->MakePacket(CMD_SC_MARKET_ITEM_SALE_LIST_V2);
	pUser->Send(pAck);

	pUser->SetState(Static::Instance().MAINFRIENDLIST());
}


} /* namespace NWait */ } /* namespace NState */ } /* namespace ProjectZ */
